﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EcoSystem.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace EcoSystem;

/// <summary>
/// 组织架构管理
/// </summary>
[Area(EcoSystemRemoteServiceConsts.ModuleName)]
[RemoteService(Name = EcoSystemRemoteServiceConsts.RemoteServiceName)]
[Route("api/Platform/Organize/organization")]
public class OrganizationController : EcoSystemController, IOrganizationAppService
{
    private readonly IOrganizationAppService _service;

    public OrganizationController(IOrganizationAppService service)
    {
        _service = service;
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<OrganizationDto> CreateAsync(CreateOrUpdateOrganizationDto input)
    {
        return await _service.CreateAsync(input);
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("{id}")]
    public async Task<OrganizationDto> GetAsync(Guid id)
    {
        return await _service.GetAsync(id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("{id}")]
    public async Task<OrganizationDto> UpdateAsync(Guid id, CreateOrUpdateOrganizationDto input)
    {
        return await _service.UpdateAsync(id, input);
    }


    /// <summary>
    /// 列表查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<PagedResultDto<OrganizationDto>> GetListAsync(GetOrganizationInput input)
    {
        return await _service.GetListAsync(input);
    }

    /// <summary>
    /// 获取所有
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("all")]
    public async Task<List<OrganizationDto>> GetAllListAsync(GetOrganizationInput input)
    {
        return await _service.GetAllListAsync(input);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("trees")]
    public async Task<List<OrganizationTreeDto>> GetTreeAsync(GetOrganizationInput input)
    {
        return await _service.GetTreeAsync(input);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("{id}")]
    public async Task DeleteAsync(Guid id)
    {
        await _service.DeleteAsync(id);
    }

    /// <summary>
    /// 添加机构用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("addUsers")]
    public async Task AddUsersAsync(AddOrganizationUsersInputDto input)
    {
        await _service.AddUsersAsync(input);
    }

    /// <summary>
    /// 机构用户列表
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("users/{id}")]
    public async Task<List<SystemUserDto>> GetUsersAsync(Guid id)
    {
        return await _service.GetUsersAsync(id);
    }

    /// <summary>
    /// 机构用户列表
    /// </summary>
    /// <param name="pathCode"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("usersForCode/{pathCode}")]
    public async Task<List<SystemUserDto>> GetUsersForCodeAsync(string pathCode)
    {
        return await _service.GetUsersForCodeAsync(pathCode);
    }

    /// <summary>
    ///   获取用户所属的部门信息列表
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("getUserDepartments/{userId}")]
    public async Task<List<UserDepartmentDto>> GetUserDepartmentsAsync(Guid userId)
    {
        return await _service.GetUserDepartmentsAsync(userId);
    }
}